#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>

#define PAGE_SIZE 4096

int main(int argc , char *argv[]) {
    int fd;
    struct stat stbuf;
    char *p_map;
    
    
    fd = open("mmap2", O_RDWR);
    if(fd < 0) {
        printf("open fail\n");
        exit(1);
    }
    fstat(fd, &stbuf);
    printf("size = %ld\n", stbuf.st_size);
    //内存映射
    p_map = (char *)mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(p_map == MAP_FAILED) {
        printf("mmap fail\n");
        munmap(p_map, stbuf.st_size);
        exit(1);
    }
    close(fd);

    char data[] = "qew";
    
    memcpy(p_map, data, strlen(data));
    printf("size = %ld\n", strlen(p_map));

    msync((void *)p_map, 4096, MS_SYNC);
    
    return 0;

}